package com.epro.dx.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.databinding.DataBindingUtil;

/**
 * 广播的监听器，用于动态注册屏幕锁屏和解锁的广播
 * @author Zenglw
 * @version 1.0
 * @date 2017/7/7
 * @see [相关类]
 */

public class ScreenBroadListener {

    private final Context mContext;
    private BroadcastReceiver mReceiver;

    public ScreenBroadListener(Context context) {
        this.mContext = context;
        mReceiver = new ScreenBroadCast();

    }

    private static class ScreenBroadCast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {//当屏幕亮起,销毁Activity
                ScreenManager.getInstance(context).finishActivity();
            } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {//当屏幕熄灭创建一个单像素的Activity
                ScreenManager.getInstance(context).startActivity();
            } else if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) { //当解锁屏幕时，销毁Activity
                ScreenManager.getInstance(context).finishActivity();
            }
        }
    }

    public void registerBroadCast(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        mContext.registerReceiver(mReceiver,filter);

    }

    public void unRegisterBroadCast(){
        mContext.unregisterReceiver(mReceiver);
    }
}
